<template>
  <div class="duorou">
    <el-row>
      <el-col :span="24">
        <el-button type="primary" @click="dialogVisible = true">添加多肉</el-button>
      </el-col>
    </el-row>
    <el-divider></el-divider>
    <el-table
      :data="duorouList"
      border
      style="width: 100%">
      <el-table-column
        prop="name"
        label="名称"
        width="180">
      </el-table-column>
      <el-table-column
        label="状态"
        width="180">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">非正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button @click="handleUpdate(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="currentDuorou.id ? '更新' : '添加'"
      :visible.sync="dialogVisible"
      width="20%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false">
      <el-form label-width="60px">
        <el-form-item label="名称">
          <el-input v-model="currentDuorou.name"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="currentDuorou.state"
            :active-value="1"
            :inactive-value="0">
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onDialogCancel">取 消</el-button>
        <el-button type="primary" @click="onDialogOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Home',

  data () {
    return {
      duorouList: [
        { id: 1, name: '多肉1', state: 0 },
        { id: 2, name: '多肉2', state: 1 },
        { id: 3, name: '多肉3', state: 1 },
        { id: 4, name: '多肉4', state: 0 },
        { id: 5, name: '多肉5', state: 1 }
      ],
      currentDuorou: {},
      dialogVisible: false
    }
  },

  created () {
    this.getDuorouList()
  },

  methods: {
    // 获取多肉列表数据
    getDuorouList () {
      this.$ajax.get('/findAllduorou').then(res => {
        // console.log(res)
        // 注意：根据真实情况，将res或其内部的数据对下一行代码打开注释并赋值
        this.duorouList = res
      })
    },

    // 根据id更新多肉
    handleUpdate (data) {
      this.currentDuorou = data
      this.dialogVisible = true
    },

    // 弹窗点击确认按钮
    onDialogOk () {
      const { currentDuorou } = this
      if (currentDuorou.id) {
        // 更新多肉
        this.$ajax.put('/updateDuorou', currentDuorou).then(res => {
          console.log(res)
          // 获取更新后的多肉列表，【备注：理想状态应该是直接修改现有数据，等待下次进入页面后再重新获取新数据，减少不必要的数据请求】
          this.getPenzaiList()
          this.currentDuorou = {} // 清空一下数据
          this.dialogVisible = false
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '更新失败!'
          })
        })
      } else {
        // 新增多肉
        this.$ajax.post('/addDuorou', currentDuorou).then(res => {
          console.log(res)
          // 获取新增后的多肉列表，【备注：理想状态应该是直接修改现有数据，等待下次进入页面后再重新获取新数据，减少不必要的数据请求】
          this.getDuorouList()
          this.currentDuorou = {} // 清空一下数据
          this.dialogVisible = false
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '新增失败!'
          })
        })
      }
    },

    // 弹窗点击取消按钮
    onDialogCancel () {
      this.currentDuorou = {}
      this.dialogVisible = false
    },

    // 根据id 删除多肉
    handleDelete (data) {
      this.$confirm(`此操作将永久删除多肉: ${data.name}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$ajax.delete('/delDuorouById', { id: data.id }).then(res => {
          console.log(res)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
